package com.derbysoft.dswitch.adapter.ihg.web.action.admin.setting.freemeal;

import com.derbysoft.dswitch.adapter.ihg.common.domain.FreeMealStrategy;
import com.derbysoft.dswitch.adapter.ihg.common.repository.FreeMealStrategyRepository;
import com.derbysoft.dswitch.adapter.ihg.web.action.support.BaseAction;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: jason
 * Date: 2013-01-06
 */
public class EditAction extends BaseAction {
    @Autowired
    private FreeMealStrategyRepository freeMealStrategyRepository;

    private FreeMealStrategy freeMealStrategy;

    private String inputError;

    @Action("editView")
    public String view() {
        if (freeMealStrategy.getId() == null) {
            return EDIT;
        }
        freeMealStrategy = freeMealStrategyRepository.load(freeMealStrategy.getId());
        return EDIT;
    }

    @Action("update")
    public String update() {
        FreeMealStrategy existed = freeMealStrategyRepository.load(freeMealStrategy.getChannelPattern(), freeMealStrategy.getHotelPattern(), freeMealStrategy.getRatePlanPattern(), freeMealStrategy.getRoomTypePattern());
        if (existed == null) {
            inputError = "FreeMealStrategy doesn't exist !!";
            return EDIT;
        }
        existed = freeMealStrategyRepository.load(freeMealStrategy.getId());
        existed.setChannelPattern(freeMealStrategy.getChannelPattern());
        existed.setHotelPattern(freeMealStrategy.getHotelPattern());
        existed.setRatePlanPattern(freeMealStrategy.getRatePlanPattern());
        existed.setRoomTypePattern(freeMealStrategy.getRoomTypePattern());
        existed.setType(freeMealStrategy.getType());
        existed.setUnit(freeMealStrategy.getUnit());
        existed.setQuantity(freeMealStrategy.getQuantity());
        existed.setDescription(freeMealStrategy.getDescription());
        freeMealStrategyRepository.save(existed);
        return LIST;
    }

    public FreeMealStrategy getFreeMealStrategy() {
        return freeMealStrategy;
    }

    public void setFreeMealStrategy(FreeMealStrategy freeMealStrategy) {
        this.freeMealStrategy = freeMealStrategy;
    }

    public String getInputError() {
        return inputError;
    }

    public void setInputError(String inputError) {
        this.inputError = inputError;
    }
}
